package edu.megatrend.fkn.rsw.ocenjivanje.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.megatrend.fkn.rsw.ocenjivanje.logika.KonfigLogika;

/**
 * Servlet implementation class Konfigurisanje
 */
public class Konfigurisanje extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Konfigurisanje() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// Smestanje svih vrednosti iz forme u properties.
		@SuppressWarnings("rawtypes")
		Enumeration paramNames = request.getParameterNames();
		Properties formValues = new Properties();

		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();

			String[] paramValues = request.getParameterValues(paramName);

			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					formValues.setProperty(paramName, paramValue);

				}
			}
		}
		
		KonfigLogika konf = new KonfigLogika(formValues);
		out.print(konf.obradi());

		/*
		 * Osnovno kofigurisanje.
		 */
		String body = "<H1>Stranica za konfigurisanje</H1>\n"
				+ "<form>"
				+ "Naziv fakulteta: <input type=\"text\" name=\"fakultet\" /><br />\n"
				+ "Adresa: <input type=\"text\" name=\"adresa\" /><br />\n"
				+ "<input type=\"submit\" value=\"Sucuvaj\" />\n" + "</form>";

		/*
		 * Dodavanje predmeta.
		 */
		body += "<H1> Dodavanje predmeta </H1>\n"
				+ "<form>"
				+ "Naziv predmeta: <input type=\"text\" name=\"naziv-predmeta\" /><br />\n"
				+ "<input type=\"submit\" value=\"Sucuvaj\" />\n" + "</form>";
		body += "<table border=1>\n";
		body += "<thead><th></th><th>Naziv predmeta</th></thead>";
		body += "<tbody>";
		body += konf.getAllPredmet(KonfigLogika.HTML_TYPE_TABLE);
		body += "</tbody>";
		body += "</table>";

		body += "<H1> Dodavanje predispitnih obaveza </H1>\n"
				+ "<form>"
				+ "Predmet: "
				+ "<select name=\"odabrani-predmet\">"
				+ konf.getAllPredmet(KonfigLogika.HTML_TYPE_SELECT)
				+ "</select>"
				+ "<p>"
				+ "Naziv predispitne obaveze: <input type=\"text\" name=\"naziv-predispitne-obaveze\" /><br />\n"
				+ "Maksimalni broj poena: <input type=\"text\" name=\"max-broj-poena\" /><br />\n"
				+ "</p>" + "Prisutnost: "
				+ "<input type=\"checkbox\" name=\"prisutnost\" />  " + "<p>"
				+ "<input type=\"submit\" value=\"Sucuvaj\" />\n" + "</p>"
				+ "</form>";

		body += konf.getTablePredispitneObaveze();

		out.println("<BODY>\n" + body + "</BODY></HTML>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
